/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package it.davial.mgwt.client.activities.menu;

import it.davial.mgwt.client.ClientFactory;
import it.davial.mgwt.client.activities.about.AboutPlace;
import it.davial.mgwt.client.activities.patient.PatientPlace;
import it.davial.mgwt.client.activities.picture.PicturePlace;
import it.davial.mgwt.client.activities.visit.VisitPlace;
import it.davial.mgwt.client.test.TestPlace;
import it.davial.mgwt.client.ui.widget.Topic;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.googlecode.mgwt.mvp.client.MGWTAbstractActivity;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedEvent;
import com.googlecode.mgwt.ui.client.widget.celllist.CellSelectedHandler;

/**
 * Activities are started and stopped by an ActivityManager associated with a container Widget.
 */
public class MenuActivity extends MGWTAbstractActivity {
	
	private static final Logger logger = Logger.getLogger("MenuActivity");
	/**
	 * Used to obtain views, eventBus, placeController.
	 * Alternatively, could be injected via GIN.
	 */
	private ClientFactory clientFactory;

	/**
	 * Sample property.
	 */

	public MenuActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		logger.info("starting MenuActivity");
		MenuView view = clientFactory.getMenuView();
	//	view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		view.setTopics(createTopicsList());

		addHandlerRegistration(view.getCellSelectedHandler().addCellSelectedHandler(new CellSelectedHandler() {

			@Override
			public void onCellSelected(CellSelectedEvent event) {
				int index = event.getIndex();
				if (index == 0) {
					clientFactory.getPlaceController().goTo(new PatientPlace());
					return;
				}
				if (index == 1) {
					clientFactory.getPlaceController().goTo(new VisitPlace());
					return;
				}
				if (index == 2) {
					clientFactory.getPlaceController().goTo(new PicturePlace());
					return;
				}
				if (index == 3) {
					clientFactory.getPlaceController().goTo(new AboutPlace());
					return;
				}
				if (index == 4) {
					clientFactory.getPlaceController().goTo(new TestPlace());
					return;
				}

			}
		}));

	}

/*	@Override
	public String mayStop() {
		return "Please hold on. This activity is stopping.";
	}
*/
	/**
	 * @see MenuView.Presenter#goTo(Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
	private List<Topic> createTopicsList() {
		ArrayList<Topic> list = new ArrayList<Topic>();
		list.add(new Topic("Patient", 5));
		list.add(new Topic("Visit", 5));
		list.add(new Topic("Picture", 5));
		list.add(new Topic("About", 5));
		list.add(new Topic("Test GUI", 5));
		return list;
	}

	

}
